package com.nova.scenic.projectlibs.util.collection;

import com.nova.scenic.projectlibs.util.debug.MyLog;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class Collections {

    private static final String TAG = Collections.class.getSimpleName();

    public static <E>List<E> toList(List<E> list,Iterator<E> iterator){
        while (iterator.hasNext()){
            E e = iterator.next();
            list.add(e);
        }
        return list;
    }

    public static <E> Collection<E> intersection(Collection<E>... collections) {
        int minSizeIndexCollection = findMinSizeForCollection(collections);
        //找出size最小的集合
        Collection<E> minSizeCollection = collections[minSizeIndexCollection];

        //遍历最小的集合
        for (E e : minSizeCollection) {

            for (int i = 0; i < collections.length; i++) {
                ///遍历所有的集合
                Collection<E> collection = collections[i];
                if (!collection.contains(e)) {
                    minSizeCollection.remove(e);
                    break;
                }
            }

        }
        return minSizeCollection;
    }

    public static <T> String showCollection(T[] t, OnShowCollectionHelper<T> helper){
        if(t == null){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (T item :t) {
            if(helper != null){
                helper.onItem(item);
                sb.append(item);
                if(helper.onCloseInternalPrint()){
                    break;
                }
            }
        }
        return sb.toString();
    }

    public static <E> String showCollection(Collection<E> collection, OnShowCollectionHelper<E> helper) {
        Iterator<E> it = collection.iterator();
        StringBuffer sb = new StringBuffer();
        while (it.hasNext()) {
            E e = it.next();
            sb.append(e);
            sb.append("\n" );
            if (helper != null) {
                helper.onItem(e);
            }
        }
        boolean internalLog = false;
        if (helper != null) {
            internalLog = !helper.onCloseInternalPrint();
        }

        if (internalLog) {
            System.out.println("collection.size is " + collection.size());
            System.out.println(sb.toString());
        }

        return sb.toString();
    }

    /**
     * 找出集合数组中，size最小的集合的索引
     *
     * @param collections
     * @return
     */
    public static <E> int findMinSizeForCollection(Collection<E>... collections) {
        if (collections != null && collections.length > 0) {
            int minIndex = 0;
            int minSizeForCollection = collections[0].size();
            for (int i = 0; i < collections.length; i++) {
                Collection<E> collection = collections[i];
                if (minSizeForCollection > collection.size()) {
                    minSizeForCollection = collection.size();
                    minIndex = i;
                }
            }
            return minIndex;
        }

        return -1;
    }

    /**
     * 找出集合数组中，size最大的集合的索引
     *
     * @param collections
     * @return
     */
    public static <E> int findMaxSizeForCollection(Collection<E>... collections) {
        if (collections != null && collections.length > 0) {
            int maxIndex = 0;
            int maxSizeForCollection = collections[0].size();
            for (int i = 0; i < collections.length; i++) {
                Collection<E> collection = collections[i];
                if (maxSizeForCollection < collection.size()) {
                    maxSizeForCollection = collection.size();
                    maxIndex = i;
                }
            }
            return maxIndex;
        }

        return -1;
    }

    public static String mapToUrl(Map<String, String> params, String paramsEncoding) {
        if (params == null) {
            return null;
        }
        StringBuilder encodedParams = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            encodedParams.append(encoding(entry.getKey(), paramsEncoding));
            encodedParams.append('=');
            encodedParams.append(encoding(entry.getValue(), paramsEncoding));
            encodedParams.append('&');
        }
        String stringParam = encodedParams.toString();
        return stringParam.substring(0, stringParam.length() - 1);
    }

    private static String encoding(String content, String encoding) {
        if (encoding != null) {
            MyLog.i(TAG, "content " + content + "  encoding " + encoding);
            try {
                if (content == null) {
                    return null;
                }
                return URLEncoder.encode(content, encoding);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return content;
    }

    public interface OnShowCollectionHelper<E> {
        void onItem(E e);

        boolean onCloseInternalPrint();
    }
}
